import 'package:flutter/material.dart';
import 'package:flutter_base/src/config/app_colors.dart';

/// 确认弹窗
class ConfirmAlterDialog {
  static void show({
    required BuildContext context,
    String title = '',
    required Widget content,
    String cancelText = '',
    String confirmText = '',
    VoidCallback? onCancel,
    required VoidCallback onConfirm,
  }) {
    if (title.isEmpty) {
      title = '提示';
    }
    if (confirmText.isEmpty) {
      confirmText = '确定';
    }
    if (cancelText.isEmpty) {
      cancelText = '取消';
    }
    showDialog(
      context: context,
      barrierDismissible: false, // 点击外部不关闭弹窗
      builder: (context) {
        return _contentWidget(
          context: context,
          title: title,
          contentWidget: content,
          confirmText: confirmText,
          cancelText: cancelText,
          onCancel: onCancel,
          onConfirm: onConfirm,
        );
      },
    );
  }

  static void showWithText({
    required BuildContext context,
    String title = '',
    required String content,
    String cancelText = '',
    String confirmText = '',
    VoidCallback? onCancel,
    required VoidCallback onConfirm,
  }) {
    if (title.isEmpty) {
      title = '提示';
    }
    if (cancelText.isEmpty) {
      cancelText = '取消';
    }
    if (confirmText.isEmpty) {
      confirmText = '确定';
    }
    showDialog(
      context: context,
      barrierDismissible: false, // 点击外部不关闭弹窗
      builder: (context) {
        return _contentWidget(
          context: context,
          title: title,
          content: content,
          cancelText: cancelText,
          confirmText: confirmText,
          onCancel: onCancel,
          onConfirm: onConfirm,
        );
      },
    );
  }

  static Widget _contentWidget({
    required BuildContext context,
    required String title,
    String? content,
    Widget? contentWidget,
    String cancelText = '',
    String confirmText = '',
    VoidCallback? onCancel,
    required VoidCallback onConfirm,
  }) {
    return AlertDialog(
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      titlePadding: const EdgeInsets.only(top: 20, bottom: 12),
      title: Text(
        title,
        textAlign: TextAlign.center,
        style: TextStyle(color: AppColors.of(context).textDarkColor, fontSize: 18, fontWeight: FontWeight.bold),
      ),
      contentPadding: const EdgeInsets.only(top: 0, bottom: 18, left: 24, right: 24),
      content:
          contentWidget ??
          Text(
            content ?? '',
            textAlign: TextAlign.center,
            style: TextStyle(color: AppColors.of(context).textDarkColor, fontSize: 14, height: 1.5),
          ),
      actionsPadding: const EdgeInsets.all(0),
      // 控制底部间距
      actions: [
        Divider(height: 0.5, thickness: 0.5, color: AppColors.of(context).lineColor),
        SizedBox(
          height: 50,
          child: Row(
            children: [
              Expanded(
                child: TextButton(
                  style: TextButton.styleFrom(
                    overlayColor: Colors.transparent, // 禁用水波纹效果
                  ),
                  onPressed:
                      onCancel ??
                      () {
                        Navigator.pop(context);
                      },
                  child: Text(
                    cancelText,
                    style: TextStyle(
                      color: AppColors.of(context).textDarkColor,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              Container(
                width: 0.5, // 分割线的宽度
                height: double.infinity, // 填充父容器的高度
                color: AppColors.of(context).lineColor, // 分割线颜色
              ),
              Expanded(
                child: TextButton(
                  style: TextButton.styleFrom(
                    overlayColor: Colors.transparent, // 禁用水波纹效果
                  ),
                  onPressed: onConfirm,
                  child: Text(
                    confirmText,
                    style: TextStyle(
                      color: AppColors.of(context).primaryColor,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}
